package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ShareKey;
import com.shaoshaohuo.app.entity.NewPickEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.NewHttpCofig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.CircleImageView;
import com.shaoshaohuo.app.utils.FiveCornerStar;
import com.shaoshaohuo.app.utils.LoginUtils;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.utils.sharesdk.ShareContent;
import com.shaoshaohuo.app.view.NumberView;
import com.shaoshaohuo.app.view.SelfadaptionImageview;
import com.shaoshaohuo.app.view.ShareDialog;
import com.shaoshaohuo.app.view.WhView;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/8.
 */

public class NewPickDetails extends BaseActivity implements WhView.OnWhlistener, View.OnClickListener {
    private ArrayList<View> views;
    private TextView fragment_detatop_iv_sc;
    private TextView textView;
    private NumberView deatils_numberView;
    private Button fragment_detalis_top_btn_gm;
    private WhView pick_whyuanjia;
    private WhView pick_whchild;
    private WhView pick_whtem;
    private Button pick_zongjia;
    private Float aFloat;
    private Float aFloat1;
    private Float aFloat2;
    private int getnum;
    private int getnum1;
    private int getnum2;
    private ImageView detalisActivity_back;
    private ImageView detalisActivity_share;
    private String catid;
    private Handler handler = new Handler();
    private TextView gm_phone;
    private TextView gm_youxiaoqi;
    private LinearLayout gm_imalist;
    private NewPickEntity top;
    private ImageView fragment_detatop_vp;
    private TextView fragment_detatop_tv_nmae;
    private TextView piuck_wifi;
    private TextView fragment_detatop_tv_detalis;
    private TextView pick_tv_yuanjia;
    private TextView pick_tv_gongxiangjia;
    private TextView pick_tv_ertongjia;
    private TextView pick_xianliang;
    private TextView pj_user_value;
    private String imaurl;
    private TextView piuck_distance;
    private String distance;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newpick_ac_layout);
        init();
        network();
    }

    private void network() {
        //解析
        Map<String, String> headerParams = RequestParam.getHeaderParams();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("catid", catid);
        Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + NewHttpCofig.App_Details, stringStringHashMap, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final NewPickEntity bottom = (NewPickEntity) o;
                top = (NewPickEntity) o;
                final NewPickEntity.DataBean data = bottom.getData();
                String name = data.getName();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        String phone = bottom.getData().getPhone();
                        gm_phone.setText(phone);
                        gm_youxiaoqi.setText(bottom.getData().getOpenTimeSta() + "至" + bottom.getData().getOpenTimeEnd() + "（周末法定节假日通用）");
                        List<NewPickEntity.DataBean.ImgDetailsBean> img_details = bottom.getData().getImg_details();
                        for (int i = 0; i < img_details.size(); i++) {
                            SelfadaptionImageview imageView = new SelfadaptionImageview(NewPickDetails.this);
                            gm_imalist.addView(imageView);
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                            layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
                            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                            float dimension = getResources().getDimension(R.dimen.imamarn1);
                            layoutParams.setMargins(0, (int) dimension, 0, 0);
                            Picasso.with(NewPickDetails.this).load(img_details.get(i).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(imageView);
                        }
//                        gm_imalist.setPadding(gm_imalist.getPaddingLeft(),gm_imalist.getPaddingTop(),gm_imalist.getPaddingRight(),gm_imalist.getBottom()+im);
                        String img = top.getData().getImg();
                        Picasso.with(NewPickDetails.this).load(img).placeholder(R.mipmap.zhanzhanzhan).into(fragment_detatop_vp);
                        fragment_detatop_tv_nmae.setText(top.getData().getName());
                        if (top.getData().getWifi().equals("1")) {
                            piuck_wifi.setText("提供WiFi");
                        } else {
                            piuck_wifi.setText("没有WiFi");
                        }
                        piuck_distance.setText("距" + distance + "km");
                        fragment_detatop_tv_detalis.setText(top.getData().getAddress());
                        pick_tv_yuanjia.setText("¥" + top.getData().getAdult() + "/人");
                        pick_tv_gongxiangjia.setText("¥" + top.getData().getTeam() + "/人");
                        pick_tv_ertongjia.setText("¥" + top.getData().getChild() + "/人");
                        pick_xianliang.setText("限量" + top.getData().getShareMax() + "人");
                        overmoney();
                        List<NewPickEntity.DataBean.RemarkBean> a = data.getRemark();
                        if (a.size() > 0) {
                            FiveCornerStar pj_user_xx = (FiveCornerStar) findViewById(R.id.pj_user_xx);
                            pj_user_xx.setIsdown(false);
                            pj_user_xx.setNum(5);
                            pj_user_xx.setdownnum(Float.parseFloat(data.getRemark().get(0).getRemarkLevel()));
                            NewPickEntity.DataBean.RemarkBean remark = a.get(0);
                            TextView pj_user_phonenum = (TextView) findViewById(R.id.pj_user_phonenum);

                            String userPhone = remark.getUserPhone();
                            String substring = userPhone.substring(3, 7);
                            String replace = userPhone.replace(substring, "****");
                            pj_user_phonenum.setText(replace);

                            TextView bcd = (TextView) findViewById(R.id.bcd);
                            String remarkPen = remark.getRemarkPen();
                            bcd.setText(remarkPen);
                            TextView pj_user_num = (TextView) findViewById(R.id.pj_user_num);
                            int remarkNum = remark.getRemarkNum();
                            pj_user_num.setText(remarkNum + "");
                            CircleImageView pj_user_photo = (CircleImageView) findViewById(R.id.pj_user_photo);
                            if (remark.getUserImg() != null || remark.getUserImg().length() != 0) {
                                Picasso.with(NewPickDetails.this).load(remark.getUserImg()).placeholder(R.mipmap.zhanzhanzhan).into(pj_user_photo);
                            }

                            String remarkValue = remark.getRemarkValue();
                            pj_user_value.setText(remarkValue);
                        } else {
                            findViewById(R.id.pj_1).setVisibility(View.GONE);
                            findViewById(R.id.pj_2).setVisibility(View.GONE);
                            pj_user_value.setText("暂无评论");
                        }
                    }
                });

            }
        }, NewPickEntity.class);
    }

    private void overmoney() {
//        pick_whyuanjia
//        pick_whchild
//        pick_whtem
        getnum2 = pick_whtem.getnum();
        getnum = pick_whyuanjia.getnum();
        getnum1 = pick_whchild.getnum();
        if (top != null) {
            aFloat = Float.valueOf(top.getData().getAdult());
            aFloat1 = Float.valueOf(top.getData().getChild());
            aFloat2 = Float.valueOf(top.getData().getTeam());
            Log.e("TAG", "overmoney: " + top.getData().getTeam());
            pick_zongjia.setText("总价：¥" + ((getnum * aFloat) + (getnum1 * aFloat1) + (getnum2 * aFloat2)));
        }


    }

    private void init() {
        piuck_distance = (TextView) findViewById(R.id.piuck_distance);
        textView = (TextView) findViewById(R.id.textView);
        ViewGroup content = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
        View childAt = content.getChildAt(0);
        detalisActivity_share = (ImageView) findViewById(R.id.detalisActivity_share);
        changeTransltestaus(this, childAt, TransltestausModel.NoePicter, Color.parseColor("#B2000000"));
        detalisActivity_back = (ImageView) findViewById(R.id.detalisActivity_back);
        detalisActivity_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        catid = intent.getStringExtra(ShareKey.CATID);
        imaurl = intent.getStringExtra("imgurl");
        distance = intent.getStringExtra("distance");
        detalisActivity_share.setOnClickListener(this);
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++//
        FiveCornerStar pick_fivestart = (FiveCornerStar) findViewById(R.id.pick_fivestart);
        pick_fivestart.setNum(5, BitmapFactory.decodeResource(getResources(), R.mipmap.star).getWidth(), BitmapFactory.decodeResource(getResources(), R.mipmap.star).getHeight());
        pick_fivestart.setIsdown(false);
        fragment_detatop_vp = (ImageView) findViewById(R.id.fragment_detatop_vp);
//        fragment_detatop_vp.getLayoutParams().height = getResources().getDisplayMetrics().heightPixels * 2 / 3 - findViewById(R.id.detalisActivity_relatopbar).getLayoutParams().height;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) findViewById(R.id.detalisActivity_relatopbar).getLayoutParams();
            layoutParams.setMargins(0, getStatusBarHeight(this), 0, 0);
        }

        fragment_detatop_tv_nmae = (TextView) findViewById(R.id.fragment_detatop_tv_nmae);

        piuck_wifi = (TextView) findViewById(R.id.piuck_wifi);

        fragment_detatop_tv_detalis = (TextView) findViewById(R.id.fragment_detatop_tv_detalis);

        pick_tv_yuanjia = (TextView) findViewById(R.id.pick_tv_yuanjia);
        //¥60.00/人
        pick_tv_gongxiangjia = (TextView) findViewById(R.id.pick_tv_gongxiangjia);
        pick_tv_ertongjia = (TextView) findViewById(R.id.pick_tv_ertongjia);
        pick_xianliang = (TextView) findViewById(R.id.pick_xianliang);
        pick_zongjia = (Button) findViewById(R.id.pick_zongjia);

        pick_whyuanjia = (WhView) findViewById(R.id.pick_whyuanjia);
        pick_whyuanjia.setWhlistener(this);
        pick_whyuanjia.setWheathermax(0);
        pick_whchild = (WhView) findViewById(R.id.pick_whchild);
        pick_whchild.setWhlistener(this);
        pick_whchild.setWheathermax(0);
        pick_whtem = (WhView) findViewById(R.id.pick_whtem);
        pick_whtem.setWheathermax(0);
        fragment_detalis_top_btn_gm = (Button) findViewById(R.id.fragment_detalis_top_btn_gm);
        fragment_detalis_top_btn_gm.setOnClickListener(this);
        pick_whtem.setWhlistener(this);

        /**
         * 评论相关
         */

        pj_user_value = (TextView) findViewById(R.id.pj_user_value);

        findViewById(R.id.pj_user_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewPickDetails.this, PjTowActivity.class);
                intent.putExtra("catid", catid);
                startActivity(intent);
            }
        });
        /*
        ===================================================华丽丽的分割线=======================================================
         */
        //collStatus 1加了0沒有
        gm_phone = (TextView) findViewById(R.id.gm_phone);

        gm_youxiaoqi = (TextView) findViewById(R.id.gm_youxiaoqi);

        gm_imalist = (LinearLayout) findViewById(R.id.gm_imalist);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ShopHttpConfig.phone());
            }
        });
    }


    @Override
    public void onwh(View view, int num, int type) {
        WhView v = (WhView) view;
        if (type == 0) {
            //增
            v.setNum(num + 1);
        } else {
            //减
            v.setNum(num - 1);
        }
        overmoney();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detalisActivity_share:
                ShareDialog shareDialog = new ShareDialog(v.getContext());
                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setTitle("妈妈爱果");
                shareParams.setTitleUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + catid);
                shareParams.setText(ShareContent.SHARE_APP_CONTENT);
                shareParams.setImageUrl(imaurl);
                shareParams.setUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + catid);
                shareDialog.setshareParams(shareParams);
                shareDialog.show();
                break;
            case R.id.fragment_detalis_top_btn_gm:
                if (LoginUtils.IsLogining()) {
                    decideTicketNum();
                } else {
                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    private void decideTicketNum() {
        if (getnum1 == 0 && getnum == 0 && getnum2 == 0) {
            ToastUtil.toast("请选择至少一张票");
        } else {
            Intent intent = new Intent();
            intent.setClass(this, SureOrderPayInformation.class);
            intent.putExtra("sorttype", "2");
            intent.putExtra("child", getnum1);
            intent.putExtra("yuanjia", getnum);
            intent.putExtra("share", getnum2);
            intent.putExtra("newpick", top);
            startActivity(intent);
        }


    }

}
